﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Data.SqlClient;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using Jxym.wlkc2011.Module.CMS.MODEL;
using Jxym.Tools.MyFile;
using System.IO;
using Jxym.wlkc2011;

namespace Jxym.wlkc2011.Module.CMS.SERVER
{
    public partial class ContentAdd : System.Web.UI.Page
    {
        private int _contentTypeID = 0;
        private IList<UpLoad_MyFileInfo> UpLoadFileList;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MsSqlDal<Cms_Content> iUtm = new MsSqlDal<Cms_Content>();
                UpLoad upload = new UpLoad();
                string siteName = "";
                if (Request.Params["siteid"] != null)
                {
                    siteName = Request.Params["siteid"];
                    string path = Server.MapPath("~/UpFiles/" + siteName);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    upload.RootPath = "~/UpFiles/" + siteName + "/";
                    upload.AllowExts = "doc|docx|ppt|pptx|pdf|jpg|png|gif|bmp|wmv|avi|flv|mp3|mp4|rar";
                    upload.IsExtClass = false;
                    upload.Save();
                    UpLoadFileList = upload.ReturnUpLoadFileInfo_List;
                }
                else
                {
                    Response.Write("{'success':false,msg:'无法获取专题名称'}");
                    return;
                }


                PropertyInfo[] fields = typeof(Cms_Content).GetProperties();
                List<string> listName = new List<string>();
                List<string> listValue = new List<string>();
                object identity = 0;
                foreach (PropertyInfo property in fields)
                {
                    if (Request.Params[property.Name] != null && property.Name != "Html")
                    {
                        listName.Add("@" + property.Name);
                        if (property.Name == "UpTime")
                        {
                            listValue.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
                        }
                        else if (property.Name == "AddTime")
                        {
                            string sj = DateTime.Now.ToString("HH:mm:ss:fff");
                            string rq = Request.Params[property.Name];
                            string rqsj = rq + " " + sj;
                            listValue.Add(rqsj);
                        }
                        else
                        {
                            listValue.Add(Request.Params[property.Name]);
                        }
                    }
                }

                listName.Add("@UpUser");
                listValue.Add(Jxym.Common.Request.AdminID("user"));
                listName.Add("@Html");
                listValue.Add(Request.Params["Html1"]);

                foreach (UpLoad_MyFileInfo fileInfo in UpLoadFileList)
                {
                    if (fileInfo.FileSize > 0)
                    {
                        if (fileInfo.FileKeyName == "FileName")
                        {
                            string filename = upload.RootPath + fileInfo.ServerFileName;
                            string extName = Path.GetExtension(filename).ToLower();
                            string fileName = Path.GetFileNameWithoutExtension(filename);

                            if (Request.Params["ContentTypeID"] != null)
                            {
                                _contentTypeID = Convert.ToInt32(Request.Params["ContentTypeID"]);
                                MsSqlDal<Cms_ContentType> cct = new MsSqlDal<Cms_ContentType>();
                                Cms_ContentType cctInfo = cct.GetModel(_contentTypeID);
                                if (cctInfo != null)
                                {
                                    if (cctInfo.TypeKey == "FLV" && (extName.IndexOf("flv") >= 0 || extName.IndexOf("mp4") >= 0))
                                    {
                                        listName.Add("@FileName");
                                        listValue.Add(fileInfo.ServerFileName);
                                    }
                                    else if (cctInfo.TypeKey == "WORD" && extName.IndexOf("doc") >= 0)
                                    {
                                        try
                                        {
                                            Jxym.Tools.Converts.OfficeConvert oc = new Tools.Converts.OfficeConvert(filename, upload.RootPath + "/" + fileName + ".pdf");
                                            bool isOk = oc.WordSaveAs(Tools.Converts.OfficeConvert.wdConvertType.wdFormatPDF);
                                            if (isOk)
                                            {
                                                File.Delete(filename);
                                                listName.Add("@FileName");
                                                listValue.Add(PDF2SWF(upload.RootPath + fileName + ".pdf"));
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Response.Write("{'success':false,msg:'WORD文档处理失败:" + ex.Message + "'}");
                                            return;
                                        }
                                    }
                                    else if (cctInfo.TypeKey == "PPT" && extName.IndexOf("ppt") >= 0)
                                    {
                                        try
                                        {
                                            Jxym.Tools.Converts.OfficeConvert oc = new Tools.Converts.OfficeConvert(filename, upload.RootPath + "/" + fileName + ".pdf");
                                            bool isOk = oc.PptSaveAs(Tools.Converts.OfficeConvert.ppConvertType.ppSaveAsPDF);
                                            if (isOk)
                                            {
                                                File.Delete(filename);
                                                listName.Add("@FileName");
                                                listValue.Add(PDF2SWF(upload.RootPath + fileName + ".pdf"));
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Response.Write("{'success':false,msg:'PPT文档处理失败:" + ex.Message + "'}");
                                            return;
                                        }
                                    }
                                    else if (cctInfo.TypeKey == "PDF" && extName.IndexOf("pdf") >= 0)
                                    {
                                        try
                                        {
                                            string rv = PDF2SWF(upload.RootPath + "/" + fileInfo.ServerFileName);
                                            if (rv.Contains(".swf"))
                                            {
                                                listName.Add("@FileName");
                                                listValue.Add(Path.ChangeExtension(fileInfo.ServerFileName, ".swf"));
                                            }
                                            else
                                            {
                                                Response.Write("{'success':false,msg:'" + rv + "'}");
                                                Response.End();
                                                return;
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Response.Write("{'success':false,msg:'PDF2SWF处理失败:" + ex.Message + "'}");
                                            return;
                                        }
                                    }
                                    else if ((cctInfo.TypeKey == "MP3" && extName.IndexOf("mp3") >= 0) || (cctInfo.TypeKey == "PHOTO" && (extName.IndexOf("jpg") >= 0 || extName.IndexOf("png") >= 0 || extName.IndexOf("gif") >= 0 || extName.IndexOf("bmp") >= 0)) || (cctInfo.TypeKey == "FLV" && extName.IndexOf("flv") >= 0) || (cctInfo.TypeKey == "WMV-AVI" && (extName.IndexOf("wmv") >= 0 || extName.IndexOf("avi") >= 0)) || (cctInfo.TypeKey == "MP3" && extName.IndexOf("mp3") >= 0))
                                    {
                                        listName.Add("@FileName");
                                        listValue.Add(fileInfo.ServerFileName);
                                    }
                                    else if (cctInfo.TypeKey == "RAR")
                                    {
                                        try
                                        {
                                            Jxym.Tools.RAR.RarHelper jtrr = new Tools.RAR.RarHelper();
                                            bool isOk = jtrr.RarDeCompress(Path.GetDirectoryName(fileInfo.ServerFileName), fileInfo.ServerFileName, false);
                                            if (isOk)
                                            {
                                                File.Delete(filename);
                                                listName.Add("@FileName");
                                                listValue.Add(PDF2SWF(upload.RootPath + fileName + ".rar"));
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Response.Write("{'success':false,msg:'PPT文档处理失败:" + ex.Message + "'}");
                                            return;
                                        }
                                    }
                                    else if (cctInfo.TypeKey == "URL" || cctInfo.TypeKey == "HTML")
                                    {
                                    }
                                    else
                                    {
                                        File.Delete(filename);
                                        Response.Write("{'success':false,msg:'附件格式与信息类型不匹配'}");
                                        return;
                                    }
                                }
                                else
                                {
                                    Response.Write("{'success':false,msg:'信息类型发生错误 info:null，数据库中没有找到匹配项'}");
                                    return;
                                }
                            }
                            else
                            {
                                Response.Write("{'success':false,msg:'请选择信息类型'}");
                                return;
                            }
                        }
                        else
                        {
                            listName.Add("@" + fileInfo.FileKeyName);
                            listValue.Add(fileInfo.ServerFileName);
                        }
                    }
                }
                if (Request.Params["id"] == null)
                {
                    listName.Add("@AddUser");
                    listValue.Add(Jxym.Common.Request.AdminID("user"));
                }
                SqlParameter[] parameters = new SqlParameter[listName.Count];
                for (int i = 0; i < listName.Count; i++)
                {
                    parameters[i] = new SqlParameter(listName[i], listValue[i]);
                }
                if (listName.Count > 0)
                {
                    if (Request.Params["id"] != null && Request.Params["id"] != "0")
                    {
                        identity = iUtm.Updata(parameters);
                        Response.Write("{'success':true,msg:'信息更新完成'}");
                    }
                    else
                    {
                        identity = iUtm.Add(parameters);
                        Response.Write("{'success':true,msg:'信息添加完成'}");
                    }
                }
                else
                {
                    Response.Write("{'success':false,'msg':'没有可提交的数据,本次操作终止'}");
                }
            }
        }

        private string PDF2SWF(string pdfFileName)
        {
            try
            {
                pdf2swf pdf2swf = new pdf2swf(Server.MapPath("~/cmd/pdf2swf.exe"), 9);
                string isOk = pdf2swf.Start(pdfFileName);
                File.Delete(pdfFileName);
                return Path.GetFileNameWithoutExtension(pdfFileName) + ".swf";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}